using System;
using System.Linq;
using System.Security.Cryptography;

namespace DAL.SecureStore
{
    public static class ProtectedDataHelper
    {
        static readonly Random Rnd = new Random();

        public static byte[] Protect(byte[] plain)
        {
            var iv = new byte[16];
            Rnd.NextBytes(iv);
            return new[]{ iv, ProtectedData.Protect(plain, iv, DataProtectionScope.CurrentUser) }.SelectMany(x => x).ToArray();
        }

        public static byte[] Unprotect(byte[] enc)
        {
            return ProtectedData.Unprotect(enc.Skip(16).ToArray(), enc.Take(16).ToArray(), DataProtectionScope.CurrentUser);
        }
    }
}